home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / promode3.zip / BBS.C < prev    next >
C/C++ Source or Header  |  1993-01-06  |  8KB  |  398 lines

  1. /*
  2.  
  3. BBS.C
  4.  
  5. This is a sample BBS program that you can use as a building block..
  6.  
  7.                     Adrian J. Michaud
  8. */
  9.  
  10. #include <conio.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <time.h>
  15. #include "promodem.h"
  16.  
  17. void main(void);
  18.  
  19. /* High Speed modem, 14.4k, use 2400 for 2400 baud modems */
  20. #define MAX_BAUD_RATE  38400
  21.  
  22. #define EXIT_BBS       1     /* Used to check if operator quits BBS */
  23.  
  24. int  InitSystemModem (void);
  25. int  WaitForCaller   (void);
  26. void MainOptionsMenu (void);
  27. void Delay           (int delay);
  28. void Modem           (char *string);
  29. int  Input           (char *buffer, int maxIn);
  30. void ChatMode        (void);
  31.  
  32. AJMS controlBlock;
  33. int  onLine = 0;   /* Set to 0 for local logon, set to 1 for remote logon */
  34.  
  35. void main(void)
  36. {
  37. int retCod;
  38. int baseAddress=0, irq=0, comPorm;
  39. char com_port[255];
  40.  
  41. while (!irq && !baseAddress)
  42.   {
  43.   printf("\nEnter Com port [1 or 2]: ");
  44.   gets(com_port);
  45.  
  46.   if (strstr(com_port, "1"))
  47.      {
  48.      baseAddress = 0x3f8;
  49.      irq          = 4;
  50.      }
  51.  
  52.   else
  53.   if (strstr(com_port, "2"))
  54.      {
  55.      baseAddress = 0x2f8;
  56.      irq          = 3;
  57.      }
  58.   }
  59.  
  60. #define BUFFER_SIZE 4096  /* Use a 4K receive IRQ Buffer */
  61.  
  62. /* Set up a communication control block that is used by all library functions */
  63. retCod = SetupControlBlock(&controlBlock, baseAddress, irq, BUFFER_SIZE);
  64. if (retCod != SUCCESSFUL)                 /* Check for errors  */
  65.    {
  66.    printf("\nUnable to set up Communication Control Block!\n");
  67.    exit(1);
  68.    }
  69.  
  70. /* Open the com port for communication */
  71. retCod = OpenCom(&controlBlock);
  72. if (retCod != SUCCESSFUL)                 /* Check for errors  */
  73.    {
  74.    printf("\nUnable to open COM port!\n");
  75.    exit(1);
  76.    }
  77.  
  78. SetBaudRate(&controlBlock, MAX_BAUD_RATE);
  79. SetDataFormat(&controlBlock, BITS_8 | STOP_BITS_1 | NO_PARITY);
  80.  
  81.  for ( ; ; )  /* Endless LOOP */
  82.  {
  83.  if (InitSystemModem() == EXIT_BBS) break;
  84.  if (WaitForCaller()   == EXIT_BBS) break;
  85.  MainOptionsMenu();
  86.  }
  87. DropDTR(&controlBlock);
  88. CloseCom(&controlBlock);
  89. }
  90.  
  91. int InitSystemModem(void)
  92. {
  93. char ch;
  94. char buffer[80];
  95. int counter=0;
  96.  
  97. printf("\nInitilizing System Modem...\n");
  98. Delay(10);
  99.  
  100. SendString(&controlBlock, "ATE1");
  101. SendCharacter(&controlBlock, 13);
  102.  
  103.    for ( ; ; )
  104.     {
  105.     ch = WaitForCharacter(&controlBlock, 8.00);
  106.     if (ch == WAIT_TIME_OUT)
  107.        {
  108.        printf("\nModem is not Responding - Try a different COM port.\n");
  109.        return(EXIT_BBS);
  110.        }
  111.     if (ch == 10)
  112.        {
  113.        buffer[counter]=0;
  114.        strupr(buffer);
  115.        if (strstr(buffer, "OK")) break;
  116.        counter=0;
  117.        }
  118.     buffer[counter++] = ch;
  119.     printf("%c", ch);
  120.     }
  121.  
  122. printf("\n");
  123. Delay(10);
  124. ClearQueue(&controlBlock);
  125. counter=0;
  126.  
  127. SendString(&controlBlock, "ATH0Q0V1E1X1S0=0S10=40&C1M1");
  128. SendCharacter(&controlBlock, 13);
  129.  
  130.    for ( ; ; )
  131.     {
  132.     ch = WaitForCharacter(&controlBlock, 8.00);
  133.     if (ch == WAIT_TIME_OUT)
  134.        {
  135.        printf("\nModem is not Responding - Try a different COM port.\n");
  136.        return(EXIT_BBS);
  137.        }
  138.     if (ch == 13)
  139.        {
  140.        buffer[counter]=0;
  141.        strupr(buffer);
  142.        if (strstr(buffer, "OK")) break;
  143.        counter=0;
  144.        }
  145.     buffer[counter++] = ch;
  146.     printf("%c", ch);
  147.     }
  148. return(0);
  149. }
  150.  
  151. int WaitForCaller(void)
  152. {
  153. int ch;
  154. char buffer[80];
  155. int  counter=0;
  156.  
  157. printf("\n\nWaiting for a call up to %ld baud.\n", MAX_BAUD_RATE);
  158. printf("\nPress [ESC] to exit BBS, space bar to logon local.\n");
  159.  
  160. printf("\nModem responses:\n");
  161. ClearQueue(&controlBlock);
  162.  
  163. for ( ; ; )
  164.    {
  165.    if (kbhit())
  166.       {
  167.       ch = (char)getch();
  168.       if (ch == 27) return(EXIT_BBS);
  169.       if (ch == 32) break;
  170.       }
  171.    if (CheckQueue(&controlBlock) == CHARACTERS_WAITING)
  172.      {
  173.      ch = GetCharacter(&controlBlock);
  174.      printf("%c", ch);
  175.      if (ch == 10)
  176.        {
  177.        buffer[counter]=0;
  178.        strupr(buffer);
  179.        if (strstr(buffer, "RING"))
  180.       {
  181.       Delay(20);
  182.       counter=0;
  183.       SendString(&controlBlock, "ATA");
  184.       SendCharacter(&controlBlock, 13);
  185.       }
  186.        if (strstr(buffer, "CONNECT"))
  187.       {
  188.       onLine=1;
  189.       break;
  190.       }
  191.        }
  192.      buffer[counter++] = ch;
  193.      }
  194.    }
  195. return(0);
  196. }
  197.  
  198. void MainOptionsMenu(void)
  199. {
  200. char buffer[255],ch;
  201. FILE *fp;
  202.  
  203. if (onLine)
  204. Delay(40);    /* Give remote users modem a chance to connect correctly */
  205.  
  206. for ( ; ; )   /* Endless Loop */
  207.  {
  208.  Modem("\n\nWelcome to a sample BBS written using ProModem!\n");
  209.  Modem("\nYour options are:\n");
  210.  Modem("\n[V]iew PROMODEM.DOC on-line!");
  211.  Modem("\n[E]nter chat mode.");
  212.  Modem("\n[H]ang-up.\n");
  213.  
  214.  Modem("\nChoice: ");
  215.  Input(buffer, 3);          /* input prompt */
  216.  if (onLine)
  217.  if (GetCarrierDetect(&controlBlock) == NO_CARRIER) break;
  218.  
  219.  strupr(buffer);            /* convert input to uppercase */
  220.  
  221.  if (!strcmp(buffer, "V"))
  222.     {
  223.     fp = fopen("PROMODEM.DOC", "r");
  224.     if (!fp)
  225.        {
  226.        Modem("\n\nPROMODEM.DOC file not found!\n");
  227.        continue;
  228.        }
  229.     while (!feof(fp))
  230.       {
  231.       if (CheckQueue(&controlBlock) == CHARACTERS_WAITING)
  232.      {
  233.      ClearQueue(&controlBlock);
  234.      break;
  235.      }
  236.       if (kbhit())
  237.     {
  238.     getch();
  239.     break;
  240.     }
  241.  
  242.       ch = fgetc(fp);
  243.       if (ch == 13) ch = 10;
  244.       if (onLine)
  245.       SendCharacter(&controlBlock, ch);
  246.       printf("%c", ch);
  247.       }
  248.     fclose(fp);
  249.     continue;
  250.     }
  251.  
  252.  if (!strcmp(buffer, "E"))
  253.     {
  254.     Modem("\nChat Mode Entered!\n");
  255.     ChatMode();
  256.     continue;
  257.     }
  258.  
  259.  if (!strcmp(buffer, "H"))
  260.     {
  261.     Modem("\n\nGoodbye!\n");
  262.     Delay(20);
  263.     DropDTR(&controlBlock);
  264.     Delay(5);
  265.     SetDTR(&controlBlock);
  266.     return;
  267.     }
  268.   } /* end of for ( ; ; ) */
  269. }
  270.  
  271. void   Delay(int timeout)
  272. {
  273. clock_t current;
  274. int total=0;
  275.  
  276. current = clock();
  277. for ( ; ; )
  278.  {
  279.  while( current == clock());
  280.  current = clock();
  281.  if (total++ >= timeout) break;
  282.  }
  283. }
  284.  
  285. void Modem(char *string)
  286. {
  287. int i,len;
  288. int temp;
  289.  
  290. len = strlen(string);
  291.  
  292. for (i=0; i<len; i++)
  293.   {
  294.   if (onLine)
  295.   SendCharacter(&controlBlock, string[i]);
  296.   printf("%c", string[i]);
  297.  
  298.   if (string[i] == 10)
  299.     {
  300.   if (onLine)
  301.   SendCharacter(&controlBlock, 13);
  302.     printf("%c", 13);
  303.     }
  304.   }
  305. }
  306.  
  307. void ChatMode(void)
  308. {
  309. unsigned char ch;
  310.  
  311. printf("\n\nSysOp: Press ESC to abort chat..\n");
  312.  
  313. for ( ; ; )
  314.    {
  315.    if (onLine)
  316.    if (GetCarrierDetect(&controlBlock) == NO_CARRIER) break;
  317.  
  318.    if (kbhit())                   /* Check if Key Press */
  319.      {
  320.      ch = (char)getch();          /* Get Character from Keyboard */
  321.      if (ch == 27) break;         /* if ESC, ABORT!              */
  322.      if (ch == 13) ch = 10;
  323.      printf("%c",ch);
  324.      if (onLine)
  325.      SendCharacter(&controlBlock, (unsigned char)ch); /* Send character to modem     */
  326.      }
  327.    if (CheckQueue(&controlBlock)==CHARACTERS_WAITING)  /* Check IRQ buffer for chars */
  328.       {
  329.       if (onLine)
  330.      {
  331.      ch = GetCharacter(&controlBlock);
  332.      if (ch == 13) ch = 10;
  333.      printf("%c", ch); /* Display Them */
  334.      SendCharacter(&controlBlock, ch);
  335.      }
  336.       }
  337.    } /* end of for ( ; ; ) */
  338. }
  339.  
  340. int Input(char *buffer, int max_in)
  341. {
  342. int ch;
  343. char backspace[10];
  344. int i=0;
  345.  
  346. ClearQueue(&controlBlock);  /* Clear any characters that might be in the queue */
  347. for ( ; ; )
  348.   {
  349.   while (!kbhit() && CheckQueue(&controlBlock) != CHARACTERS_WAITING)
  350.       {
  351.       if (onLine)
  352.       if (GetCarrierDetect(&controlBlock) == NO_CARRIER)
  353.      {
  354.      buffer[0] = 0;
  355.      return(0);
  356.      }
  357.       }   /* Wait for a key press from modem or keyboard */
  358.  
  359.   if (kbhit())            /* Get Keypress from keyboard */
  360.     ch = getch();
  361.   else
  362.    if (onLine)           /* Get character from modem */
  363.     ch = GetCharacter(&controlBlock);
  364.  
  365.   if (ch == 8 && i == 0) continue;
  366.  
  367.   if (ch == 8) {
  368.            i--;
  369.            sprintf(backspace, "%c%c%c",8,32,8);
  370.            Modem(backspace);
  371.            continue;
  372.            }
  373.  
  374.   if (ch == 13) {
  375.         buffer[i] = 0;
  376.         i=0;
  377.         break;
  378.         }
  379.  
  380.   if (max_in > 1 && i == max_in-1) continue;
  381.  
  382.   if (ch <32) continue;
  383.   i++;
  384.  
  385.   printf("%c",ch);                   /* Display character to screen */
  386.   if (onLine)
  387.   SendCharacter(&controlBlock, (unsigned char)ch);  /* Send character to modem */
  388.   buffer[i-1] =(char)ch;
  389.  
  390.   if (max_in == 1) {
  391.            buffer[i] = 0;
  392.            break;
  393.            }
  394.   }
  395. return(0);
  396. }
  397.  
  398.